package com.gs.synedocket.dialog;

import android.content.Context;
import android.graphics.Bitmap;

import androidx.annotation.NonNull;

import com.gs.synedocket.R;
import com.gs.synedocket.control.SignView;
import com.gs.synedocket.util.ImageUtil;
import com.gs.synedocket.util.ToastUtil;


public class SignDialog extends BaseDialog {

    private Context context;
    public SignView viewSign;

    public SignDialog(@NonNull Context context, IDoneListener iDoneListener) {
        super(context, R.style.custom_dialog);
        this.context = context;
        this.iDoneListener = iDoneListener;
    }

    @Override
    public void refreshView() {
    }

    @Override
    public void initView() {
        viewSign = findViewById(R.id.view_sign);
        findViewById(R.id.iv_close).setOnClickListener(v -> dismiss());
        findViewById(R.id.btn_clear).setOnClickListener(v -> viewSign.clear());
        findViewById(R.id.btn_confirm).setOnClickListener(v -> {
            if (!viewSign.isDraw()) {
                ToastUtil.showToast(R.string.no_sign);
                return;
            }
            if (iDoneListener != null) {
                iDoneListener.getDoneFromDialog(getSignPhoto());
            }
            dismiss();
        });
    }

    @Override
    public int getLayoutId() {
        return R.layout.dialog_sign;
    }

    private IDoneListener iDoneListener;

    public interface IDoneListener {
        void getDoneFromDialog(String signPhoto);
    }

    private String getSignPhoto() {
        String url = "";
        viewSign.setBackgroundColor(context.getResources().getColor(R.color.white));
        Bitmap bitmap = ImageUtil.getViewBitmap(viewSign);
        if (bitmap != null) {
            url = ImageUtil.bitmapToBase64(bitmap);
//            url = ImageUtil.bitmapToUrl(context, bitmap);
        }
        viewSign.setBackgroundColor(context.getResources().getColor(R.color.color_ed));
        return url;
    }

}
